package com.heer.gymnasium.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.heer.amap.utils.ToastUtil;
import com.heer.gymnasium.AppHelper;
import com.heer.gymnasium.R;
import com.heer.gymnasium.constant.Extras;
import com.heer.gymnasium.module.login.views.LoginActivity;
import com.heer.gymnasium.util.ActivityStartUtils;
import com.heer.gymnasium.util.KeyBoardUtils;
import com.heer.gymnasium.util.TitleUtils;

import butterknife.ButterKnife;
//import cn.sharesdk.framework.ShareSDK;

/**
 * Created by heer on 16/6/1.
 */
public class BaseActivity extends AppCompatActivity implements Extras {
    protected String TAG;

    protected TitleUtils titleUtils;
    protected static Context mContext;
    protected AppHelper mAppHelper;
    protected ActivityStartUtils activityStartUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        initView();
        AppHelper.addActivity(this);
    }

    protected void init() {
        TAG = getComponentName().getClassName();

        Log.e(TAG, "init: ");

        mContext = this;
        mAppHelper = AppHelper.context();
        activityStartUtils = new ActivityStartUtils(this);
//        ShareSDK.initSDK(mContext);
    }

    protected void initView() {
        Log.e(TAG, "initView: ");
        ButterKnife.bind(this);
        setActionBarLayout(true, R.layout.layout_title_bar, this);
    }

    protected void loadData() {
        loadData(false);
    }

    protected void loadData(boolean loadMore) {
        Log.e(TAG, "loadData: ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG, "onStop: ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy: ");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG, "onStart: ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData();
        Log.e(TAG, "onResume: ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG, "onPause: ");
//        KeyBoardUtils.closeKeyboardWithoutFinish(this);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.e(TAG, "onRestoreInstanceState: ");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e(TAG, "onRestart: ");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.e(TAG, "onSaveInstanceState: ");
    }

    /**
     * 设置ActionBar的布局
     *
     * @param layoutId 布局Id
     */
    @SuppressLint("NewApi")
    public void setActionBarLayout(boolean isShowBar, int layoutId, Context mContext) {
        if (isShowBar) {
            ActionBar actionBar = getSupportActionBar();
            if (null != actionBar) {
                LayoutInflater inflater = LayoutInflater.from(mContext);
                View v = inflater.inflate(layoutId, new LinearLayout(mContext), false);
                ActionBar.LayoutParams layout = new ActionBar.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                actionBar.setCustomView(v, layout);
                actionBar.setDisplayShowHomeEnabled(false);
                actionBar.setDisplayShowCustomEnabled(true);
                actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
                titleUtils = new TitleUtils(v);
                titleUtils.setLeftOnClick(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();

                    }
                });
            }
        } else {
            ActionBar actionBar = getSupportActionBar();
            if (null != actionBar) {
                actionBar.hide();
            }
        }
    }

    protected void toLogin() {
        Intent intent = new Intent(mContext, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    //token过期跳转
    public static void timeout() {
        LoginActivity.logout(mContext, LoginActivity.TIME_OUT);
    }

    //请求失败的Toast通知
    public static void requestErrorToast(String errorMessage) {
        ToastUtil.show(mContext, errorMessage);
    }
}
